package project3;
/**
 * 客户端程序 ShortestPath.java 求解一个单源点最短路径问题，并使用图形绘制了结果。
 * */
import edu.princeton.cs.algs4.*;
import project3.dijkstra.DijkstraSp3;

import javax.swing.*;
import java.awt.*;
import java.io.File;

import static project3.Test3.init_Graph2;
import static project3.Test3.init_Point;

public class ShortestPath extends JFrame {
    private EdgeWeightedDigraph mDigraph;
    private Point[] points;
    private int s;
    private int d;

    public void setmDigraph(EdgeWeightedDigraph mDigraph) {
        this.mDigraph = mDigraph;
    }

    public void setPoints(Point[] points) {
        this.points = points;
    }

    public void setS(int s) {
        this.s = s;
    }

    public void setD(int d) {
        this.d = d;
    }

    public ShortestPath(){
        setSize(800, 800);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
    }



    @Override
    public void paint(Graphics g){
        //画点
        g.setColor(Color.GREEN);
        g.setColor(Color.GREEN);
        g.setFont(new Font("楷体", Font.BOLD, 20));
        //g.drawString("起点", (int) points[0].getX(), (int)points[0].getY());
        //g.drawString("汇聚点", (int) points[points.length-1].getX(), (int)points[points.length-1].getY());
        for(int i = 0; i < points.length;i++){
            g.drawString(i+"", (int) points[i].getX(), (int)points[i].getY());
        }
        for(int i = 0;i < points.length;i++){
            for (DirectedEdge edge:mDigraph.adj(i)) {
                g.drawLine(
                        (int) points[edge.from()].getX(),
                        (int) points[edge.from()].getY(),
                        (int) points[edge.to()].getX(),
                        (int) points[edge.to()].getY());
            }
        }
            DijkstraSp3 msp = new DijkstraSp3(mDigraph,points);
            //Long cutime =System.currentTimeMillis() * 1000;
            //long start_time = System.currentTimeMillis();
            msp.setFrom(s);
            msp.setTo(d);
            boolean b = msp.hasPathTo(d);
            if (b) {
                //long consumingtime=System.currentTimeMillis()-start_time;
                //System.out.println("运行时间："+consumingtime+"ms");
                g.setColor(Color.RED);
                Graphics2D g2 = (Graphics2D)g;  //g是Graphics对象
                g2.setStroke(new BasicStroke(3.0f));
                //System.out.println("最短路径为：");
                for (DirectedEdge edge : msp.pathTo(d)) {
                    g2.drawLine(
                            (int)points[edge.from()].getX(),
                            (int)points[edge.from()].getY(),
                            (int)points[edge.to()].getX(),
                            (int)points[edge.to()].getY());

                }
            } else {
                //long consumingtime=System.currentTimeMillis()-start_time;
                //System.out.println("运行时间："+consumingtime+"ms");
                System.out.println("不存在一条路径");
            }

    }
    public static void main(String[] args) {
        File file = new File("D:/my_code/Algorithm4/AlgorithmHomework/resources/data.txt");
        In in = new In(file);
        int a = in.readInt();//点数
        int b = in.readInt();//边数
        EdgeWeightedDigraph mDigraph = new EdgeWeightedDigraph(a);
        Point[] points = new Point[a];
        init_Point(in, points);
        init_Graph2(in, b, mDigraph, points);
            int s = 0;
            int d = 5;
        //int s = 0;
        //int d = 10;
            ShortestPath shortestPath = new ShortestPath();
            shortestPath.setPoints(points);
            shortestPath.setS(s);
            shortestPath.setD(d);
            shortestPath.setmDigraph(mDigraph);
            shortestPath.setVisible(true);
    }
}
